﻿@page "/Account/Login"

@inject ILogger<Login> Logger
@inject NavigationManager NavigationManager
@inject ApiService ApiService
<h1>Log in</h1>
<div class="row">
    <div class="col-md-4">
        <section>
            <StatusMessage Message="@errorMessage" />
            <h2>Use a local account to log in.</h2>
            <hr />
            <div class="form-floating mb-3">
                <InputText @bind-Value="UserId" class="form-control" autocomplete="username" aria-required="true" placeholder="name@example.com" />
                <label for="userId" class="form-label">用户名</label>
            </div>
            <div class="form-floating mb-3">
                <InputText type="password" @bind-Value="Password" class="form-control" autocomplete="current-password" aria-required="true" placeholder="password" />
                <label for="password" class="form-label">密码</label>
            </div>
            <div>
                <button type="button" @onclick="LoginUser" class="w-100 btn btn-lg btn-primary">Log in</button>
            </div>
        </section>
    </div>
</div>



@code {
    private string? errorMessage;

    public string UserId { get; set; } = default!;
    // [SupplyParameterFromForm]
    private string Password { get; set; } = default!;

    [SupplyParameterFromQuery]
    private string? ReturnUrl { get; set; }

    // protected override async Task OnInitializedAsync()
    // {
    //     // Clear the existing external cookie to ensure a clean login process
    //     await HttpContext.SignOutAsync(IdentityConstants.ExternalScheme);
    // }

    public async Task LoginUser()
    {
        if (!await ApiService.Login(UserId, Password))
        {
            errorMessage = "Error:用户名或者密码错误！";
            return;
        }
        NavigationManager.NavigateTo(ReturnUrl ?? "/");

    }

    
}
